package org.csu.jpetstorespringversion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetstorespringversion.mapper.AccountMapper;
import org.csu.jpetstorespringversion.mapper.ItemMapper;
import org.csu.jpetstorespringversion.mapper.OrdersMapper;
import org.csu.jpetstorespringversion.model.*;
import org.csu.jpetstorespringversion.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.net.http.HttpRequest;
import java.util.List;

@Controller
public class OrderCotroller {
    /**创建新订单界面**/
    private static final String NEW_ORDER = "html/order/NewOrderForm.html";

    /**登录注册界面**/
    private static final String SIGNONFORM = "html/account/SignonForm.html";
    /**错误界面**/
    private static final String ERROR = "html/common/Error.html";
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private Order order;

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private Account account;

    @Autowired
    private Cart cart;

    @RequestMapping("/newOrderForm")
    public String newOrderForm(
            HttpSession session
            /**
             * 在Spring MVC中，Controller方法中可以包含一个类型为HttpSession的参数。
             * 这个参数是用来获取当前请求的HttpSession对象的。
             * 在其他Controller中可以使用相同的HttpSession对象获取该属性的值。
             */
    ){
//        account = (Account) session.getAttribute("account");
//        cart = (Cart)session.getAttribute("cart");
        /**
         * 测试一下，新建一个account和cart
         */
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid","j2ee");
        List<Account> accountList = accountMapper.selectList(queryWrapper);
        account = accountList.get(0);
        QueryWrapper<Item> queryWrapper1 = new QueryWrapper<Item>();
        queryWrapper1.eq("itemId","CSG-1");
        List<Item> itemList = itemMapper.selectList(queryWrapper1);
        Item item = new Item();
        item = itemList.get(0);
        CartItem cartItem = new CartItem(item,1,true,new BigDecimal("1"));
        cart.addItem(item,true);
        /**
         * 以上为测试代码
         */
        System.out.println("in newOrder cart quantity is:"+cart.getCartItemList().get(0).getQuantity());
        if (account == null){
            System.out.println("account is null");
            session.setAttribute("message", "You must sign on before attempting to check out.  Please sign on and try checking out again.");
            //request.getRequestDispatcher(SIGNONFORM).forward(request, response);
            return SIGNONFORM;
        } else if (cart != null) {
            order.initOrder(account,cart);
            System.out.println("this order linequantity in newOrderForm is: "+order.getLineItems().get(0).getQuantity());
            //订单放入session，这里其实就是已经生成订单了，但是还不是我们应该改库存的地方。
            session.setAttribute("order", order);
            /**
             * 日志还没放入
             */
             return NEW_ORDER;
        }else{
            /**购物车为空的情况**/
            session.setAttribute("message", "An order could not be created because a cart could not be found.");
            //request.getRequestDispatcher(ERROR).forward(request, response);
            return ERROR;
        }
    }


}
